package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.lang.Console;
import com.gc.common.base.Result;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.support.Convert;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.domain.SysRole;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.UpkeepInfo;
import com.gc.business.service.IUpkeepInfoService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯保养 控制层
 * 
 * @author hruiyan
 * @date 2020-11-18
 */
@Slf4j
@Controller
@RequestMapping("/business/upkeepInfo")
public class UpkeepInfoController extends BaseController {
    private String prefix = "business/upkeepInfo";
	
	private final IUpkeepInfoService upkeepInfoService;

	@Autowired
	private ISysUserService userService;

	@Autowired
	public UpkeepInfoController(IUpkeepInfoService upkeepInfoService) {
		this.upkeepInfoService = upkeepInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:upkeepInfo:view")
	@GetMapping("/{type}")
	public String upkeepInfo(@PathVariable("type")Integer type,ModelMap mmap){
		mmap.put("typeL",type);
	    return prefix + "/upkeepInfo";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{upkeepId}")
	public String edit(@PathVariable("upkeepId") Long upkeepId, ModelMap mmap){
		UpkeepInfo upkeepInfo = upkeepInfoService.getById(upkeepId);
		mmap.put("upkeepInfo", upkeepInfo);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯保养列表
	 * @Param typeL ： 0. 待保养 ，1.待确认  2.已完成
	 */
	@RequiresPermissions("business:upkeepInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(UpkeepInfo upkeepInfo,Integer typeL){
		Long attrId = getAttrId();
		Boolean ifCompany = userService.ifCompanyRole(attrId);
		if(ifCompany){
			startPage();
			upkeepInfo.setApplicant(ShiroUtils.getUserId()+"");
			List<UpkeepInfo> list = upkeepInfoService.selectListByReviewType(upkeepInfo,typeL,attrId);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<UpkeepInfo>()));
		}
	}

	/**
	 * 新增保存电梯保养
	 */
	@RequiresPermissions("business:upkeepInfo:add")
	@Log(title = "电梯保养", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(UpkeepInfo upkeepInfo){
		upkeepInfoService.save(upkeepInfo);
		return Result.success();
	}

	/**
	 * 修改保存电梯保养
	 */
	@RequiresPermissions("business:upkeepInfo:edit")
	@Log(title = "电梯保养", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(UpkeepInfo upkeepInfo){
		upkeepInfoService.updateById(upkeepInfo);
		return Result.success();
	}
	
	/**
	 * 删除电梯保养
	 */
	@RequiresPermissions("business:upkeepInfo:remove")
	@Log(title = "电梯保养", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		upkeepInfoService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯保养列表
	 */
	@RequiresPermissions("business:upkeepInfo:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(UpkeepInfo upkeepInfo){
		List<UpkeepInfo> list = upkeepInfoService.list(upkeepInfo);
		ExcelUtil<UpkeepInfo> util = new ExcelUtil<>(UpkeepInfo.class);
		util.exportExcel(list, "upkeepInfo");
		return Result.success();
	}
}
